/*
 *  Copyright (C) 2022  贺龙宇
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include <ctime>
#include <mutex>

#include "../Serial/SerialPort.h"
#include <opencv2/opencv.hpp>
#include "../define.h"
#include "../Log/log.h"

#define SERIAL_NORMAL 0
#define SERIAL_ABNOMAL 1

class Protocol {
public:
    explicit Protocol(SerialPort& serial_port);/*初始化，传入参数为已经初始化完成的串口*/
    ~Protocol();
    void sendTarget(cv::Point3f& target); /*外部调用，发送数据*/
    void receiveData();  /*外部调用，接收数据*/

private:
    SerialPort& serial_port_;
};

enum class EnemyColor : uchar { RED,
    BLUE };/*敌方颜色，红色和蓝色*/
enum class State : uchar { ARMOR_STATE,
    RUNE_SMALL,RUNE_BIG,TOPPING_STATE};/*识别状态：自瞄，小能量机关，大能量机关*/

/*敌方装甲板目标*/
struct Targets {
    float yaw; /*偏航*/
    float pitch;/*俯仰*/
    float distance;/*距离*/
};

/*接收下位机数据结构，可参考通讯协议*/
struct McuData {
    uint8_t id; /*兵种id*/
    EnemyColor color;/*敌方颜色（即识别颜色）*/
    State state; /*识别状态*/
    uint8_t shoot_speed; /*当前射速*/
    short gimbal_yaw; /*云台偏航数值*/
    short gimbal_pitch; /*云台俯仰数值*/
};

struct GimbalData{
    int yaw;
    int pitch;
};

/*握手数据，目前已弃用，但仍保留*/
struct HandData
{
    uint8_t id;
    EnemyColor enemy_color;
};

// int serial_states;

extern int serial_states;
extern McuData mcu_data;
extern HandData hand_data;
// extern std::mutex protocol_lock;
extern GimbalData gimbal_data;

extern bool serial_first_receive; /*用于判断串口是否第一次收到数据*/